package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotUpkeepPlanBo;
import org.dromara.iot.domain.vo.IotUpkeepPlanVo;
import org.dromara.iot.service.IIotUpkeepPlanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯保养计划
 *
 * @author lsl
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/upkeepPlan")
public class IotUpkeepPlanController extends BaseController {

    private final IIotUpkeepPlanService iotUpkeepPlanService;

    /**
     * 查询电梯保养计划列表
     */
    @SaCheckPermission("iot:upkeepPlan:list")
    @GetMapping("/list")
    public TableDataInfo<IotUpkeepPlanVo> list(IotUpkeepPlanBo bo, PageQuery pageQuery) {
        return iotUpkeepPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯保养计划列表
     */
    @SaCheckPermission("iot:upkeepPlan:export")
    @Log(title = "电梯保养计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotUpkeepPlanBo bo, HttpServletResponse response) {
        List<IotUpkeepPlanVo> list = iotUpkeepPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯保养计划", IotUpkeepPlanVo.class, response);
    }

    /**
     * 获取电梯保养计划详细信息
     *
     * @param planId 主键
     */
    @SaCheckPermission("iot:upkeepPlan:query")
    @GetMapping("/{planId}")
    public R<IotUpkeepPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long planId) {
        return R.ok(iotUpkeepPlanService.queryById(planId));
    }

    /**
     * 新增电梯保养计划
     */
    @SaCheckPermission("iot:upkeepPlan:add")
    @Log(title = "电梯保养计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotUpkeepPlanBo bo) {
        return toAjax(iotUpkeepPlanService.insertByBo(bo));
    }

    /**
     * 修改电梯保养计划
     */
    @SaCheckPermission("iot:upkeepPlan:edit")
    @Log(title = "电梯保养计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotUpkeepPlanBo bo) {
        return toAjax(iotUpkeepPlanService.updateByBo(bo));
    }

    /**
     * 删除电梯保养计划
     *
     * @param planIds 主键串
     */
    @SaCheckPermission("iot:upkeepPlan:remove")
    @Log(title = "电梯保养计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] planIds) {
        return toAjax(iotUpkeepPlanService.deleteWithValidByIds(List.of(planIds), true));
    }
}
